/**
 * 
 */
package org.o14x.alpha.ui.commands;

import java.io.File;
import java.util.Map;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.menus.UIElement;
import org.o14x.alpha.services.FileServices;
import org.o14x.alpha.ui.controller.UIController;

/**
 * Paste command
 * 
 * @author Olivier DANGREAUX
 */
public class PasteCommand extends AbstractHandler implements IElementUpdater {
	private UIController uiController;

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// target directory is current directory
		File targetDirectory = uiController.getDomain().getCurrentExplorerTab().getFolder();

		// gets paths of file to be copied from clipboard
		Clipboard cb = new Clipboard(PlatformUI.getWorkbench().getDisplay());
		FileTransfer fileTransfer = FileTransfer.getInstance();
		String[] selectedPaths = (String[]) cb.getContents(fileTransfer);

		FileServices fileServices = uiController.getServices().getFileServices();

		// files to be copied
		File[] copiedFiles = fileServices.toFiles(selectedPaths);

		// copy operation
		if (!uiController.getDomain().getClipboard().isCut()) {
			// if copied files are from the target directory
			if (copiedFiles[0].getParentFile().equals(targetDirectory)) {
				// modify the name of the files before copy
				fileServices.duplicate(copiedFiles, targetDirectory);
			} else {
				// copy files without modifying their names
				fileServices.copy(copiedFiles, targetDirectory, false, true);
			}
		} 
		// cut operation
		else {
			// if copied files are not from the target directory
			if (!copiedFiles[0].getParentFile().equals(targetDirectory)) {
				// moves files
				fileServices.move(copiedFiles, targetDirectory, false, true);
			}
			uiController.getDomain().getClipboard().setCut(false);
		}

		// refresh ui
		uiController.getDomain().getCurrentExplorerTab().getFolderView().update();
		uiController.getUi().getFolderTreeView().update();

		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
	 */
	@Override
	public boolean isEnabled() {
		boolean isEnabled = false;

		isEnabled = uiController.getDomain().getClipboard().hasFileContent();

		return isEnabled;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateElement(UIElement element, Map parameters) {
		fireHandlerChanged(new HandlerEvent(this, true, false));
	}

	/*******************************************************************************
	 * GETTERS AND SETTERS
	 ******************************************************************************/

	/**
	 * Returns the value of uiController.
	 *
	 * @return The value of uiController.
	 */
	public UIController getUiController() {
		return uiController;
	}

	/**
	 * Sets the value of uiController.
	 *
	 * @param uiController The value of uiController to set.
	 */
	public void setUiController(UIController uiController) {
		this.uiController = uiController;
	}
}
